
package com.shop.cloud.live.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.core.util.SensitiveUtils;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.common.security.annotation.Inside;
import com.shop.cloud.live.admin.service.LiveConfigService;
import com.shop.cloud.live.common.entity.LiveConfig;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 直播配置
 *
 * @author
 * @date 2025-01-09 10:03:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/liveconfig")
@Tag(description = "liveconfig", name = "直播配置管理")
public class LiveConfigController {

    private final LiveConfigService liveConfigService;

    /**
     * 直播配置查询
     *
     * @return R
     */
    @Operation(summary = "直播配置查询")
    @GetMapping
    @PreAuthorize("@ato.hasAuthority('live:liveconfig:get')")
    public R get() {
        LiveConfig liveConfig = liveConfigService.getOne(Wrappers.emptyWrapper());
        if (liveConfig != null) {
            //数据脱敏
            String pullAuthKey = SensitiveUtils.process(liveConfig.getPullAuthKey());
            liveConfig.setPullAuthKey(pullAuthKey);
            String pushAuthKey = SensitiveUtils.process(liveConfig.getPushAuthKey());
            liveConfig.setPushAuthKey(pushAuthKey);
        }
        return R.ok(liveConfig);
    }

    /**
     * 直播配置修改/新增
     *
     * @param liveConfig 直播配置
     * @return R
     */
    @Operation(summary = "直播配置修改/新增")
    @SysLog("修改直播配置/新增")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('live:liveconfig:edit')")
    public R updateById(@RequestBody LiveConfig liveConfig) {
        if (StrUtil.isBlank(liveConfig.getId())) {
            liveConfigService.save(liveConfig);
        } else {
            //数据脱敏
            LiveConfig liveConfigOld = liveConfigService.getById(liveConfig.getId());
            String pullAuthKey = liveConfigOld.getPullAuthKey();
            String pushAuthKey = liveConfigOld.getPushAuthKey();
            if (StrUtil.isNotBlank(pullAuthKey)) {
                String sensitivePullAuthKey = SensitiveUtils.process(pullAuthKey);
                String sensitivePushAuthKey = SensitiveUtils.process(pushAuthKey);
                if (!sensitivePullAuthKey.equals(liveConfig.getPullAuthKey())) {
                    pullAuthKey = liveConfig.getPullAuthKey();
                }
                if (!sensitivePushAuthKey.equals(liveConfig.getPushAuthKey())) {
                    pushAuthKey = liveConfig.getPushAuthKey();
                }
                liveConfig.setPullAuthKey(pullAuthKey);
                liveConfig.setPushAuthKey(pushAuthKey);
            }

            liveConfigService.updateById(liveConfig);
        }
        return R.ok(liveConfig);
    }

    /**
     * 直播配置查询（服务间调用）
     *
     * @return R
     */
    @Operation(summary = "直播配置查询（服务间调用）")
    @GetMapping("/inside")
    @Inside
    public R getInside() {
        LiveConfig liveConfig = liveConfigService.getByTenantId(TenantContextHolder.getTenantId());
        if (liveConfig != null) {
            //数据脱敏
            liveConfig.setPushAuthKey(null);
            liveConfig.setPullAuthKey(null);
        }
        return R.ok(liveConfig);
    }
}
